#include <iostream>
#include <algorithm>
using namespace std;
// f(i,j)表示区间[i,i+2^j-1]的最大值
// f(i,0)=arr[i]
// f(i,j) = max(f(i,j-1),f(i+2^{j-1},j-1))
// 查询时对于每个询问[l,r]把它分成
// f[l,l+2^s-1]和f[r-2^s+1,r]
// s = int(log(r-l+1))
const int logn = 21;
const int maxn = 2000001;
int f[maxn][logn], Logn[maxn];
inline int read()
{
    char c = getchar();
    int x = 0, f = 1;
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
void pre()
{
    Logn[1] = 0;
    Logn[2] = 1;
    for (int i = 3; i < maxn; ++i)
        Logn[i] = Logn[i / 2] + 1;
}
int main()
{
    int n = read(), m = read();
    for (int i = 1; i <= n; ++i)
        f[i][0] = read();
    pre();
    for (int j = 1; j <= logn; j++)
        for (int i = 1; i + (1 << j) - 1 <= n; ++i)
            f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
    for (int i = 1; i <= m; ++i)
    {
        int x = read(), y = read();
        int s = Logn[y - x + 1];
        printf("%d\n", max(f[x][s], f[y - (1 << s) + 1][s]));
    }
    return 0;
}